package com.dongdongshop.service.impl;

import com.dongdongshop.data.Result;
import com.dongdongshop.mapper.TbSpecificationMapper;
import com.dongdongshop.mapper.TbSpecificationOptionMapper;
import com.dongdongshop.model.TbSpecification;
import com.dongdongshop.model.TbSpecificationExample;
import com.dongdongshop.model.TbSpecificationOption;
import com.dongdongshop.model.TbSpecificationOptionExample;
import com.dongdongshop.service.SpecService;
import com.dongdongshop.vo.SpecVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;


@Service
public class SpecServiceImpl implements SpecService {

    @Resource
    private TbSpecificationMapper specificationMapper;

    @Resource
    private TbSpecificationOptionMapper TbSpecificationOptionMapper;


    @Override
    public List<SpecVo> SpecList(SpecVo specVo) {
        TbSpecificationExample example = new TbSpecificationExample();
        TbSpecificationExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(specVo.getSpecName())) {
            criteria.andSpecNameLike("%" + specVo.getSpecName() + "%");
        }

        List<TbSpecification> speclist = specificationMapper.selectByExample(example);
        List<SpecVo> collect = speclist.stream().map(spec -> {
            SpecVo vo = new SpecVo();
            BeanUtils.copyProperties(spec, vo);
            return vo;
        }).collect(Collectors.toList());
        return collect;
    }



    @Override
    public void add(SpecVo specVo, List<TbSpecificationOption> options) {
        TbSpecification spec = new TbSpecification();
        BeanUtils.copyProperties(specVo,spec);
        specificationMapper.addById(spec);

        //保存规格并返回主键
        for (TbSpecificationOption option : options) {
            option.setSpecId(spec.getId());
            TbSpecificationOptionMapper.insertSelective(option);
        }

    }


    //删除
    @Override
    public void delete(Long[] ids) {
        TbSpecificationExample example = new TbSpecificationExample();
        TbSpecificationExample.Criteria criteria = example.createCriteria();
        List<Long> longs = Arrays.asList(ids);
        criteria.andIdIn(longs);
        specificationMapper.deleteByExample(example);

        TbSpecificationOptionExample example1 = new TbSpecificationOptionExample();
        TbSpecificationOptionExample.Criteria criteria1 = example1.createCriteria();
        criteria1.andSpecIdIn(longs);
        TbSpecificationOptionMapper.deleteByExample(example1);
    }

    @Override
    public SpecVo getBySpecId(Long id) {
        SpecVo vo = new SpecVo();
        TbSpecification tbSpecification = specificationMapper.selectByPrimaryKey(id);
        BeanUtils.copyProperties(tbSpecification,vo);

        TbSpecificationOptionExample example = new TbSpecificationOptionExample();
        TbSpecificationOptionExample.Criteria criteria = example.createCriteria();
        criteria.andSpecIdEqualTo(id);
        List<TbSpecificationOption> tbSpecificationOptions = TbSpecificationOptionMapper.selectByExample(example);
        vo.setSpecOptionList(tbSpecificationOptions);
        return vo;
    }



    @Override
    public void update(TbSpecification specification, List<TbSpecificationOption> list) {
            specificationMapper.updateByPrimaryKeySelective(specification);

            for (TbSpecificationOption option : list) {
                TbSpecificationOptionMapper.updateByPrimaryKeySelective(option);
            }

    }



    @Override
    public List<SpecVo> getspec() {
        List<TbSpecification> list = specificationMapper.selectByExample(null);
        List<SpecVo> collect = list.stream().map(spec -> {
            SpecVo vo = new SpecVo();
            BeanUtils.copyProperties(spec, vo);
            return vo;
        }).collect(Collectors.toList());
        return collect;
    }
}
